package org.softmed.rest.admin.module;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import org.softmed.rest.admin.DetailChooser;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.rest.admin.interfaces.Validator;
import org.softmed.rest.admin.resource.ResourceEditor;

import restlet.description.convert.RESTResourceDescription;


public class ModuleResourceManager extends JPanel implements Validator,
		EventListener, SelectionPathChanged {

	List<ResourceEditor> resourceEditors = new ArrayList<ResourceEditor>();
	List<RESTResourceDescription> resources;
	FileHandler fileHandler;

	ModuleEditor editor;

	public ModuleResourceManager() {
		// setLayout(new GridLayout(0,1));
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setBackground(Color.BLUE);
		// setBorder(BorderFactory.createLineBorder(Color.BLUE));

		// setMinimumSize(new Dimension(540, 25));

	}

	public void refresh() {
		removeAll();
		for (RESTResourceDescription resource : resources) {
			ResourceEditor editor = new ResourceEditor();
			editor.setResourceDescription(resource);
			addEditor(editor);
		}
	}

	public void addEditor(ResourceEditor editor) {
		editor.setEventListener(this);
		editor.setValidator(this);
		editor.setFileHandler(fileHandler);
		add(editor);
		resourceEditors.add(editor);
//		editor.setDetailLevel(DetailLevel.HANDLER);
		revalidate();
	}

	@Override
	public void listen(Object source) {

		ResourceEditor editor = (ResourceEditor) source;
		remove(editor);
		resourceEditors.remove(editor);
		resources.remove(editor.getResourceDescription());
		revalidate();

	}

	@Override
	public boolean validate(Object object) {
		ResourceEditor editor = (ResourceEditor) object;
		String url = editor.getResourceDescription().getUri();
		if (url == null)
			return true;

		for (RESTResourceDescription description : resources) {
			if (description.getUri().equals(url))
				return false;
		}

		return true;
	}

	public void addNewResource() {
		RESTResourceDescription resourceDescription = new RESTResourceDescription();
		resources.add(resourceDescription);
		ResourceEditor editor = new ResourceEditor();
		editor.setResourceDescription(resourceDescription);
		addEditor(editor);
		if (this.editor.getDetailLevel().compareTo(DetailLevel.RESOURCE) <= 0)
			this.editor.setDetailLevel(DetailLevel.RESOURCE);
		editor.setDetailLevel(this.editor.getDetailLevel());
		

	}

	public List<RESTResourceDescription> getResources() {
		return resources;
	}

	public void setResources(List<RESTResourceDescription> resources) {
		this.resources = resources;
		refresh();
	}

	public void validateData() throws ValidationException {
		for (ResourceEditor editor : resourceEditors) {
			editor.validateData();
		}

	}

	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		for (ResourceEditor editor : resourceEditors) {
			editor.selectionPathChanged(directory, name, parent);
		}

	}

	public FileHandler getFileHandler() {
		return fileHandler;
	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
		for (ResourceEditor editor : resourceEditors) {
			editor.setFileHandler(fileHandler);
		}
	}

	public void setDetailLevel(DetailLevel detail) {
		for (ResourceEditor editor : resourceEditors) {
			editor.setDetailLevel(detail);
		}
	}

	public ModuleEditor getEditor() {
		return editor;
	}

	public void setEditor(ModuleEditor editor) {
		this.editor = editor;
	}
}
